package cn.com.service.impl;

import cn.com.dao.RoomDao;
import cn.com.dao.impl.RoomDaoImpl;
import cn.com.domain.PageBean;
import cn.com.domain.Room;
import cn.com.service.RoomService;
import cn.com.utils.JDBCutils;
import org.apache.commons.dbutils.QueryRunner;

import java.util.List;

public class RoomServiceImpl implements RoomService {
    private RoomDao dao=new RoomDaoImpl();
    @Override
    public PageBean<Room> findRoomByPage(String _currentPage, String _rows) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        //创建空的PageBean对象
        PageBean<Room> pb= new PageBean<Room>();
        //设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //调用dao查询总记录数
        int totalCount=dao.findroomTotalCount();
        pb.setTotalCount(totalCount);
        //调用dao查询List集合
        //计算开始的记录索引
        int start=(currentPage-1)*rows;
        List<Room> list=dao.findroomByPage(start,rows);
        pb.setList(list);
        //计算总页码 比如总共5页 就是总数除以记录数字，能除尽就是totalCount/rows 不然加1
        int totalPage=totalCount % rows  == 0 ? totalCount/rows : (totalCount/rows)+1;
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public List<Room> findAll() {
        List<Room> rooms = dao.findAll();
        return rooms;
    }

    @Override
    public Room findRoombyroom_id(int id) {
        return dao.findRoombyroom_id(id);
    }

    @Override
    public void alterCustomerbyid(Room room, String id) {
        dao.alterCustomerbyid(room,id);
    }

    @Override
    public void addRoom(Room room) {
        dao.addRoom(room);
    }

    @Override
    public void delteRoombyid(String ruid) {
        dao.delteRoombyid(ruid);
    }

}
